package com.inspur.dgov.tenant.runner;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.inspur.dgov.common.boot.operation.RedissonCollection;
import com.inspur.dgov.common.tool.constant.AppConstant;
import com.inspur.dgov.tenant.entity.PubTenant;
import com.inspur.dgov.tenant.service.PubTenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 系统启动预处理
 *
 * @Author chiyupei
 * @Date 20/3/28 下午2:53
 * @Version 1.0
 * @Description 系统启动预处理
 */
@Component
@Order(1)
public class TenantStartupRunner implements CommandLineRunner {

    @Autowired
    private RedissonCollection redissonCollection;

    @Autowired
    private PubTenantService pubTenantService;

    /**
     * 初始化租户信息到Redis中
     * @param args
     * @throws Exception
     */
    @Override
    public void run(String... args) throws Exception {
        //从数据库获取所有租户
        QueryWrapper<PubTenant> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(PubTenant::getStatus, AppConstant.DB_STATUS_NORMAL);
        List<PubTenant> tenantList = this.pubTenantService.list(queryWrapper);

        List<PubTenant> tenantRList = this.redissonCollection.getList(AppConstant.DEFAULT_TENANT_KEY);
        if (null != tenantRList) {
            this.redissonCollection.deleteList(AppConstant.DEFAULT_TENANT_KEY);
        }
        //更新租户信息到redis中
        this.redissonCollection.setListValues(AppConstant.DEFAULT_TENANT_KEY, tenantList, Long.valueOf(60 * 60 * 12 * 1000));
    }
}
